package org.peoples.ui.video {
	
	import com.qualityattributes.ui.DrawObject;
	
	import flash.display.Sprite;
	import flash.media.Camera;
	import flash.media.Video;
	import flash.net.NetStream;
	
	import mx.containers.Canvas;
	
	public class UserVideo extends Canvas {
		
		private var mc_video:Video;
		private var mc_mask:Sprite;
		private var _smoothing:Boolean;
		private var _stream:NetStream;
		
		public function UserVideo():void {
			super();
			
			this._smoothing = true;
		}
		
		public function attachCamera(camera:Camera):void {
			mc_video.clear();
			mc_video.attachCamera(camera);
		}
		
		public function attachNetStream(netStream:NetStream):void {
			mc_video.clear();
			_stream = netStream;
			mc_video.attachNetStream(netStream);
		}
		
		override protected function commitProperties():void {
			super.commitProperties();
			
			mc_video.smoothing = _smoothing;
		}
		
		override protected function createChildren():void {
			super.createChildren();
			
			if (!mc_video) {
				mc_video = new Video();
				this.rawChildren.addChild(mc_video);
			}
			
			if (!mc_mask) {
				mc_mask = new Sprite;
				
				mc_video.mask = mc_mask;
				
				this.rawChildren.addChild(mc_mask);
			}
		}
		
		override protected function measure():void {
			super.measure();
			
			measuredWidth = 188;
			measuredHeight = 141;
		}
		
		override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void {
			super.updateDisplayList(unscaledWidth, unscaledHeight);
			
			var drawer:DrawObject = new DrawObject(mc_mask.graphics);
			drawer.clear();
			drawer.beginFill(0xff0000);
			drawer.drawRoundRect(0, 0, this.width, this.height, this.getStyle("cornerRadius")*2, this.getStyle("cornerRadius")*2);
				
			mc_video.width = this.width;
			mc_video.height = this.height;
			
			mc_mask.width = mc_video.width;
			mc_mask.height = mc_video.height;
		}
		
		public function get smoothing():Boolean {
			return this._smoothing;
		}
		public function set smoothing(value:Boolean):void {
			this._smoothing = value;
			invalidateProperties();
		}
		
		public function get stream():NetStream {
			return this._stream;
		}
		
	}
	
}